<?php session_start();?>
<?php require_once('Connections/database.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO danhmuc (TenDanhMuc, ThuTu, Xoa) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['TenDanhMuc'], "text"),
                       GetSQLValueString($_POST['SortOrder'], "int"),
					   0);

  mysql_select_db($database_database, $database);
  $Result1 = mysql_query($insertSQL, $database) or die(mysql_error());

  $insertGoTo = "DanhMuc.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
if(!isset($_SESSION['user']))
{
	header("Location: login.php");	
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi-vn" lang="vi-vn">
<head>
    <title>Admin</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link href="Content/style.css" rel="stylesheet" type="text/css" />
    <link href="Content/jquery-ui-1.9.2.custom.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.9.2.custom.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="Scripts/ckeditor/ckeditor.js" type="text/javascript"></script>
    <script src="Scripts/ckeditor/samples/sample.js"></script>
	<script type="text/javascript" src="Editor/scripts/innovaeditor.js"></script>
	
    <script type="text/javascript">
        $(document).ready(function () {
            $('.dropdown').hover(function () {
                $(this).find('.sub_navigation').toggle();
            });
        });

        function MenuClick(url) {
            window.location.href = url;
        }
    </script>
   <script type="text/javascript">
    $(document).ready(function () {
        $("#MenuDanhMuc").removeClass("dropdown").addClass("active");
    });
</script>
</head>
<body>
    <div id="wrap">
        <div id="header">
            <img alt="dat nuoc viet logo" src="Content/Images/gift_logo_20.png" height="100px"/>
        </div>
        <div id="menu">
            <ul id="navigation">
                <li class="dropdown" id="MenuHome"><a href="index.php" class="menuitem">Trang chủ</a></li>
                <li class="dropdown" id="MenuDanhMuc"><a href="DanhMuc.php" class="menuitem">Danh mục</a>
                </li>
                <li class="dropdown" id="MenuSanPham"><a href="SanPham.php" class="menuitem">Sản phẩm</a>
                </li>
                <li class="dropdown" id="MenuCP"><a href="thaydoimatkhau.php" class="menuitem">Thay đổi mật khẩu</a></li>
            </ul>
        </div>

        <div id="date">
            <div class="datecontrol">
                <a href="../index.php" target="_blank">Trang chủ</a>&nbsp;&nbsp;&nbsp;
                <a href="logout.php">Đăng xuất</a>
            </div>
            <div class="clear">&nbsp;</div>
        </div>
        <div id="content">
			<div class="contenttitle">
                <div class="titleplace">
                    <span>Thêm mới 1 danh mục sản phẩm</span>&nbsp;&nbsp;&nbsp;
                </div>
                <div class="buttonplace">
            <a href="DanhMuc.php">
                        <input type="button" value="Danh mục sản phẩm" />
                    </a>
                </div>
                <div class="clear">&nbsp;</div>
            </div>
            <div class="contentitem">
            	<?php
					mysql_select_db($database_database, $database);
					$query_DanhMuc = "SELECT * FROM danhmuc WHERE Xoa = false";
					$DanhMuc = mysql_query($query_DanhMuc, $database) or die(mysql_error());
					$totalRows_DanhMuc = mysql_num_rows($DanhMuc);
				?>
  				<form method="POST" action="<?php echo $editFormAction; ?>" name="form">       
                    <div class="atributelabel">Tên danh mục <span style="color:Red;">(*)</span>:</div>
                    <div class="atributeeditor"><input type="text" class="input250" name="TenDanhMuc" id="TenDanhMuc"/></div>
                    <div class="clear">&nbsp;</div>
                    
                    <div class="atributelabel">Thứ tự <span style="color:Red;">(*)</span>:</div>
                    <div class="atributeeditor"><input type="text" class="input250" name="SortOrder" id="SortOrder" value="<?php echo $totalRows_DanhMuc + 1; ?>"/></div>
                    <div class="clear">&nbsp;</div>
                    
                    <div class="atributelabel">&nbsp;</div>
                    <div class="atributeeditor">
                        <input type="submit" value="Lưu lại"/>
                    </div>
                    <div class="clear">&nbsp;</div>
                    <input type="hidden" name="MM_insert" value="form">
          		</form>
            </div>

        </div>      
    </div>  
</body>
</html>
